import routes from "./routes";
import util from '@/libs/framework/util'

let addRoute = function () {
    routes.forEach(e => {
        let beforeEnter = (to, from, next, reject) => {
            // plus中阻止进入404页面
            if (window.env == 'plus' && to.path.includes('www/index.html')) {
                reject()
                // util.$framework.redirect.home(true)
                return
            }

            if (window.env == 'um') {
                if (umSdk.status != 'ready') {
                    umSdk.config({
                        id: 1428638126334676889
                    })
                    umSdk.ready(function (bridge) {
                        window.plus.isReady = true

                        umSdk.setCanInterceptBackKey({
                            canIntercept: true, // true 拦截，false 不拦截
                            success: function () {
                                // 移除监听
                                umSdk.off('notifyOnBackPressed', notifyOnBackPressed)
                                // 开启监听
                                umSdk.on('notifyOnBackPressed', notifyOnBackPressed);
                                next();
                            },
                            fail: function (err) {
                                next();
                            }
                        })
                    })
                } else {
                    umSdk.setCanInterceptBackKey({
                        canIntercept: true, // true 拦截，false 不拦截
                        success: function () {
                            // 移除监听
                            umSdk.off('notifyOnBackPressed', notifyOnBackPressed)
                            // 开启监听
                            umSdk.on('notifyOnBackPressed', notifyOnBackPressed);
                            next();
                        },
                        fail: function (err) {
                            next();
                        }
                    })
                }
            } else {
                next();
            }
        };
        let beforeLeave = (to, from, next) => {
            window.localStorage.setItem("rh_beforeLeave_to", to.path);
            if(util.isUmSdk()){
                if (to.path == '/zjApp' || to.path == '/' || to.path == '/business/qzjx/index') {
                    let title = '起重机械'
                    if(to.query && to.query.menuCode && (to.query.menuCode == 'zj_qzjx_mobileEquipment' || to.query.menuCode == 'zj_qzjx_mobileEquipmentStatistics')){
                        window.localStorage.setItem("rh_to.query", to.query.menuCode);
                        title = '移动机械'
                    }
                    localStorage.setItem('hidden','0')
                    umSdk.setNavigation({
                        title: title, // 导航栏标题
                        hidden: 0, // 0显示导航栏 1隐藏导航栏，默认显示
                        type: 1, // 导航栏类型（1:一级导航，2:二级导航）默认：2
                        success: function () { },
                        fail: function (err) { }
                    });
                } else if (to.path == '/ajzApp'){
                    if (window.plus && window.plus.webview) {
                        let webview = window.plus.webview.getWebviewById("ajzPage");
                        if (webview) {
                            webview.close();
                        }
                    }
                } else {
                    umSdk.setNavigation({
                        title: '起重机械', // 导航栏标题
                        hidden: 1, // 0显示导航栏 1隐藏导航栏，默认显示
                        type: 1, // 导航栏类型（1:一级导航，2:二级导航）默认：2
                        success: function () { },
                        fail: function (err) { }
                    });
                }
            }
            // if (to.path == '/zjApp' || to.path == '/' || to.path == '/business/qzjx/index') {
            //     console.log(11);
            // } else {
            //     console.log(22);
            // }
            if (window.env == 'um') {

                if (to.path == '/zjApp' || to.path == '/') {
                    umSdk.preRefresh({
                        type: 1, // 0刷新 1不刷新
                        step: 0, // 返回上第几页，默认0
                        success: function () { },
                        fail: function (err) { }
                    })
                } else {
                    next();
                }
            } else {
                next();
            }
        }
        e.beforeLeave = beforeLeave;
        e.beforeEnter = beforeEnter;
    })
}
let notifyOnBackPressed = function notifyOnBackPressed (data, responseCallback) {
    var searchbar = util.$f7.searchbar.get();
    if (searchbar && searchbar.enabled) {
        searchbar.disable();
        responseCallback({ isConsumed: true }) // 返回true 则APP不会执行返回操作
        return;
    }
    var sheet = util.$f7.sheet.get();
    if (sheet) {
        sheet.close();
        responseCallback({ isConsumed: true })
        return;
    }
    var dialog = util.$f7.dialog.get();
    if (dialog) {
        dialog.close();
        responseCallback({ isConsumed: true }) // 返回true 则APP不会执行返回操作
        return;
    }
    var actions = util.$f7.actions.get();
    if (actions) {
        actions.close();
        responseCallback({ isConsumed: true }) // 返回true 则APP不会执行返回操作
        return;
    }

    var popover = util.$f7.popover.get();
    if (popover) {
        popover.close();
        responseCallback({ isConsumed: true }) // 返回true 则APP不会执行返回操作
        return;
    }

    var picker = util.$f7.picker.get();
    if (picker && picker.opened) {
        picker.close();
        responseCallback({ isConsumed: true }) // 返回true 则APP不会执行返回操作
        return;
    }

    var popup = util.$f7.popup.get();
    if (popup) {
        popup.close();
        responseCallback({ isConsumed: true }) // 返回true 则APP不会执行返回操作
        return;
    }

    var smartSelect = util.$f7.smartSelect.get();
    if (smartSelect && smartSelect.opened) {
        smartSelect.close();
        responseCallback({ isConsumed: true }) // 返回true 则APP不会执行返回操作
        return;
    }

    responseCallback({ isConsumed: false }) // 返回true 则APP不会执行返回操作
}
addRoute();
export default routes
